package gw.com.android.app.dispatch;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Base64;

import java.util.Map;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigSettingDeal;
import gw.com.android.model.ConfigType;
import gw.com.android.presenter.LoginPresenter;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.onlive.MainLiveFragment;
import www.com.library.app.AppActivities;
import www.com.library.app.ObjectSessionStore;

/**
 * 分发“其他”模块的业务处理类
 * hxgts2://other/
 *
 * @author jett
 * @since 2018-05-17.
 */
public class OtherDispatch implements Dispatch {

    @Override
    public boolean handle(Activity activity, String path, Map<String, String> params) {
        if ("qqservice".equals(path)) {
            qqService(activity, params);
            return true;
        } else if ("login".equals(path)) {
            login(activity);
            return true;
        } else if ("autologin".equals(path)) {
            autoLogin(activity, params);
            return true;
        } else if ("jiepan_tab".equals(path)) {
            toJiepanTab();
            return true;
        } else if ("browser".equals(path)) {
            toSystemBrowser(activity, params);
            return true;
        } else if ("close_self".equals(path)) {
            closeSelf(activity);
            return true;
        }
        return false;
    }

    /**
     * hxgts2://other/jiepan_tab/
     */
    private void toJiepanTab() {
        AppActivities.getSingleton().popAllActivityExceptOne(MainActivity.class.getSimpleName());
        MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
        if (mainActivity != null) {
            mainActivity.setTabFragment(ConfigType.TAB_ONLIVE_TAG, String.valueOf(MainLiveFragment.JIEPAN));
        }
    }

    /**
     * hxgts2://other/login/
     */
    private void login(Activity context) {
        GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_REAL);
        ActivityManager.backLogin(context);
    }

    /**
     * hxgts2://other/autologin?uid=xxx&pw=xxx
     */
    private void autoLogin(Activity context, Map<String, String> params) {
        if (params != null) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> me : params.entrySet()) {
                sb.append(me.getKey()).append("=").append(me.getValue());
            }
            LoginPresenter mPresenter = new LoginPresenter(context);
            mPresenter.autoLogin(sb.toString(), (BaseActivity) context);
        }
    }

    /**
     * hxgts2://other/qqservice/
     * 打开QQ客服
     */
    private void qqService(Activity context, Map<String, String> params) {
        String source;
        if (params != null && params.containsKey("source")) {
            source = params.get("source");
        } else {
            source = ConfigType.QQ_SOURCE_APP;
        }
        ConfigSettingDeal mDeal = new ConfigSettingDeal();
        mDeal.openQQ(context, source);
    }

    /**
     * hxgts2://other/close_self/
     */
    private void closeSelf(Activity activity) {
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
        }
    }

    /**
     * hxgts2://other/browser?link=aHR0cHM6Ly9zb2Z0Lmh4OTk5OS5jb20vR1RTMi9jaHJvbWUuYXBr
     * 跳转到手机系统浏览器，link需Base64加密
     */
    private void toSystemBrowser(Activity activity, Map<String, String> params) {
        if (params == null || params.isEmpty()) {
            return;
        }
        String link = params.get("link");
        if (TextUtils.isEmpty(link)) {
            return;
        }
        String url = new String(Base64.decode(link, Base64.NO_WRAP));
        Uri uri = Uri.parse(url);
        Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
        activity.startActivity(viewIntent);
    }

}
